/**
 * Created by zhangchuanguo on 2016/12/6.
 */


var util = require('../utils/util.js');
var daoUtil = require('../utils/daoUtil');
var dicStockClass = require('../class/dic-stock.js');

module.exports = {

    create: function (params, cb) {
        var models = util.models;
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }

        var dicStock = new dicStockClass();

        var checkResult = dicStock.checkParams(
            [
                'name',
                'stockCode'
            ],
            params);
        if (checkResult) {
            cb(null, util.getErrorRes('400', checkResult));
            return;
        } else {
            dicStock.init(params);
        }

        models.dic_stock.findOrCreate(dicStock.getParams(), function (err, data) {
            if (err || !data) {
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }

            cb(null, util.getSuccessRes(data));
        });
    },

    getList: function (params, cb) {
        var models = util.models;

        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }
        var rowIds = params.rowIds;
        var name = params.name;
        var expr = {limit:10};
        if(name) {
            expr.or = [{name:{'like': '%' + name + '%'}},{stockcode:{'like': '%' + name + '%'}}];
        }

        if(rowIds){
            expr.rowid   = { '!' : JSON.parse(rowIds) }
        }
        models.dic_stock.find(expr).exec(function (err, data) {
            if (err || !data) {
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }

            cb(null, util.getSuccessRes(data));

        });

    },
    getAllList: function (params, cb) {
        var models = util.models;

        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }
        var expr = {limit:30};

        models.dic_stock.find(expr).exec(function (err, data) {
            if (err || !data) {
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }

            cb(null, util.getSuccessRes(data));

        });

    },
    getStockListInStockPool: function (params, cb) {
        daoUtil.proxy(function(models){

            var name = params.name;
            var expr = {limit:10};
            if(name) {
                expr.or = [{name:{'like': '%' + name + '%'}},{stockCode:{'like': '%' + name + '%'}}];
            }


            var sql = "select b.rowId,b.stockCode,b.name from tb_stock_channel a,tb_dic_stock b where a.stockId = b.rowId and (b.name like '%"+name+"%' or b.stockCode like '%"+name+"%') and a.channelType = 'stockEstimate' and a.enable = 1";
            models.dic_stock.query(sql,function(err, data) {
                if (err || !data) {
                    console.error(err);
                    cb(util.getSysError());
                }else{
                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    },
    getStockByCode:function (params,cb) {
        var validate = daoUtil.validate(['stockCode'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        daoUtil.proxy(function(models){
            models.dic_stock.findOne({stockCode:params.stockCode}).exec(function (err, data) {
                if (err) {
                    console.error(err);
                    cb(util.getSysError());
                }else{
                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    }
};



